deferred class IUP_DRAG_AND_DROP
-- Commands related to drag & drop.

inherit
	IUP_WIDGET_INTERNALS
	
insert
	IUP_INTERFACE

feature {ANY}

	-- Attributes at Drag Source

	set_drag_cursor (name: STRING)
		-- (non inheritable): name of an image to be used as cursor during drag. 
		-- See also IUP_IMAGE.
		do
			iup_open.set_attribute(Current, "DRAGCURSOR", name)
		end

	set_drag_source (state: BOOLEAN)
		-- (non inheritable): Set up a control as a source for drag operations. 
		-- Default:	False.
		do
			iup_open.set_attribute(Current, "DRAGSOURCE", boolean_to_yesno(state))
		end

	set_drag_types (types: STRING)
		-- (non inheritable): A list of data types that are supported by the 
		-- source. Accepts a string with one or more names separated by commas. 
		-- Must be set.
		--
		-- Drag and Drop support can be set independently. A control can have 
		-- drop without drag support and vice-versa.
		--
		-- Here are some common Drag&Drop types defined by existing applications:
		--
		-- * "TEXT" used for regular text without formatting. Automatically 
		--   translated to CF_TEXT in Windows.
		-- * content MIME types, like "text/uri-list", "text/html", "image/png", 
		--   "image/jpeg", "image/bmp" and "image/gif".
		-- * "UTF8_STRING" in GTK and "UNICODETEXT" in Windows.
		-- * "COMPOUND_TEXT" in GTK and "Rich Text Format" in Windows.
		-- * "BITMAP" and "DIB" in Windows. Automatically translated to CF_BITMAP 
		--   and CF_DIB.
		do
			iup_open.set_attribute(Current, "DRAGTYPES", types)
		end

	set_drag_source_move (state: BOOLEAN)
		-- (non inheritable): Enables the move action. Default: False (only copy 
		-- is enabled).
		do
			iup_open.set_attribute(Current, "DRAGSOURCEMOVE",
										  boolean_to_yesno(state))
		end

	-- Attributes at Drop Target

	set_drop_target (state: BOOLEAN)
		-- (non inheritable): Set up a control as a destination for drop 
		-- operations. Default: False.
		do
			iup_open.set_attribute(Current, "DROPTARGET",
										  boolean_to_yesno(state))
		end

	set_drop_types (types: STRING)
		-- (non inheritable): A list of data types that are supported by the 
		-- target. Accepts a string with one or more names separated by commas. 
		-- Must be set.
		--
		-- Drag and Drop support can be set independently. A control can have 
		-- drop without drag support and vice-versa.
		--
		-- Here are some common Drag&Drop types defined by existing applications:
		--
		-- * "TEXT" used for regular text without formatting. Automatically 
		--   translated to CF_TEXT in Windows.
		-- * content MIME types, like "text/uri-list", "text/html", "image/png", 
		--   "image/jpeg", "image/bmp" and "image/gif".
		-- * "UTF8_STRING" in GTK and "UNICODETEXT" in Windows.
		-- * "COMPOUND_TEXT" in GTK and "Rich Text Format" in Windows.
		-- * "BITMAP" and "DIB" in Windows. Automatically translated to CF_BITMAP 
		--   and CF_DIB.
		do
			iup_open.set_attribute(Current, "DROPTYPES", types)
		end

	-- Callbacks at Drag Source (Must be set when DRAGSOURCE=True)

	set_cb_drag_begin (act: FUNCTION[TUPLE[IUP_WIDGET, INTEGER, INTEGER], STRING])
		-- Notifies source that drag started. It is called when the mouse starts 
		-- a drag operation.
		--
		-- ih: identifier of the element that activated the event.
		-- x, y: cursor position relative to the top-left corner of the element.
		--
		-- Returns: If IUP_IGNORE is returned the drag is aborted.
		local
			operation: INTEGER
		do
			cb_dragbegin := act
			
			if cb_dragbegin /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DRAGBEGIN_CB", "NONEEDED", operation)
		end

	set_cb_drag_data_size (act: FUNCTION[TUPLE[IUP_WIDGET, STRING], INTEGER])
		-- Request for size of drag data from source. It is called when the data 
		-- is dropped, before the DRAGDATA_CB callback.
		--
		-- ih: identifier of the element that activated the event.
		-- type: type of the data. It is one of the registered types in DRAGTYPES.
		--
		-- Returns: the size in bytes for the data. It will be used to allocate 
		-- the buffer size for the data in transfer.
		local
			operation: INTEGER
		do
			cb_dragdatasize := act
			
			if cb_dragdatasize /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DRAGDATASIZE_CB", "NONEEDED", operation)
		end

	set_cb_drag_data (act: FUNCTION[TUPLE[IUP_WIDGET, STRING, POINTER, INTEGER], STRING])
		-- Request for drag data from source. It is called when the data is 
		-- dropped.
		--
		-- ih: identifier of the element that activated the event.
		-- type: type of the data. It is one of the registered types in DRAGTYPES.
		-- data: buffer to be filled by the application.
		-- size: buffer size in bytes. The same value returned by 
		-- DRAGDATASIZE_CB.	
		local
			operation: INTEGER
		do
			cb_dragdata := act
			
			if cb_dragdata /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DRAGDATA_CB", "NONEEDED", operation)
		end

	set_cb_drag_end (act: FUNCTION[TUPLE[IUP_WIDGET, INTEGER], STRING])
		-- Notifies source that drag is done. The only drag callback that is 
		-- optional. It is called after the data has been dropped.
		--
		-- ih: identifier of the element that activated the event.
		-- action: action performed by the operation (1 = move, 0 = copy, -1 = 
		-- drag failed or aborted)
		--
		-- If action is 1 it is responsibility of the application to remove the 
		-- data from source.
		local
			operation: INTEGER
		do
			cb_dragend := act
			
			if cb_dragend /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DRAGEND_CB", "NONEEDED", operation)
		end

	-- Callbacks at Drop Target (Must be set when DROPTARGET=True)

	set_cb_drop_data (act: FUNCTION[TUPLE[IUP_WIDGET, STRING, POINTER, INTEGER, INTEGER, INTEGER], STRING])
		-- Source has sent target the requested data. It is called when the data 
		-- is dropped. If both drag and drop would be in the same application it 
		-- would be called after the DRAGDATA_CB callback.
		--
		-- ih: identifier of the element that activated the event.
		-- type: type of the data. It is one of the registered types in DROPTYPES.
		-- data: content data received in the drop operation.  In Lua is a light 
		-- userdata.
		-- size: data size in bytes.
		-- x, y: cursor position relative to the top-left corner of the element.
		local
			operation: INTEGER
		do
			cb_dropdata := act
			
			if cb_dropdata /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DROPDATA_CB", "NONEEDED", operation)
		end

	set_cb_drop_motion (act: FUNCTION[TUPLE[IUP_WIDGET, INTEGER, INTEGER, STRING], STRING])
		-- Notifies destination about drag pointer motion. The only drop callback 
		-- that is optional. It is called when the mouse moves over any valid 
		-- drop site.
		--
		-- ih: identifier of the element that activated the event.
		-- x, y: position in the canvas where the event has occurred, in pixels.
		-- status: status of mouse buttons and certain keyboard keys at the 
		-- moment the event was generated. The same macros used for BUTTON_CB can 
		-- be used for this status.
		local
			operation: INTEGER
		do
			cb_dropmotion := act
			
			if cb_dropmotion /= Void then
				operation := 1
			else
				operation := 0
			end
						
			iup_open.set_callback (Current, "DROPMOTION_CB", "NONEEDED", operation)
		end

feature {IUP}

	-- Drag callbacks

	execute_dragbegin (x, y: INTEGER): STRING
		do
			Result := cb_dragbegin.item([Current, x, y])
		end

	execute_dragdatasize (type: STRING): INTEGER
		do
			Result := cb_dragdatasize.item([Current, type])
		end

	execute_dragdata (type: STRING; data: POINTER; size: INTEGER): STRING
		do
			Result := cb_dragdata.item([Current, type, data, size])
		end

	execute_dragend (action: INTEGER): STRING
		do
			Result := cb_dragend.item([Current, action])
		end

	-- Drop callbacks

	execute_dropdata (type: STRING; data: POINTER; size, x, y: INTEGER): STRING
		do
			Result := cb_dropdata.item([Current, type, data, size, x, y])
		end

	execute_dropmotion (x, y: INTEGER; status: STRING): STRING
		do
			Result := cb_dropmotion.item([Current, x, y, status])
		end

feature {}

	cb_dragbegin: FUNCTION[TUPLE[IUP_WIDGET, INTEGER, INTEGER], STRING]
	cb_dragdatasize: FUNCTION[TUPLE[IUP_WIDGET, STRING], INTEGER]
	cb_dragdata: FUNCTION[TUPLE[IUP_WIDGET, STRING, POINTER, INTEGER], STRING]
	cb_dragend: FUNCTION[TUPLE[IUP_WIDGET, INTEGER], STRING]

	cb_dropdata: FUNCTION[TUPLE[IUP_WIDGET, STRING, POINTER, INTEGER, INTEGER, INTEGER], STRING]
	cb_dropmotion: FUNCTION[TUPLE[IUP_WIDGET, INTEGER, INTEGER, STRING], STRING]

end

-- The MIT License (MIT)

-- Copyright (c) 2016 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
